﻿//
//日期：2011-11-10
//说明：处理缓存

using System;
using System.Net;
using System.Collections.Generic;

namespace WISD.Common
{
    public class StaticCache<TKey, TValue>
    {
        /// <summary>
        /// 缓存
        /// </summary>
        private static Dictionary<string, Dictionary<TKey, TValue>> CacheDic = new Dictionary<string, Dictionary<TKey, TValue>>();
        
        /// <summary>
        /// 缓存的Key
        /// </summary>
        private string CacheKey;

        private StaticCache() { }       

        /// <summary>
        /// 获得缓存处理对象，并以指定的Key初始化
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        public static StaticCache<TKey, TValue> GetInstance(string cacheKey)
        {
            StaticCache<TKey, TValue> _cache = new StaticCache<TKey, TValue>();            
            _cache.Init(cacheKey);
            return _cache;
        }

        /// <summary>
        /// 初始化缓存Key
        /// </summary>
        /// <param name="cacheKey"></param>
        private void Init(string cacheKey)
        {
            CacheKey = cacheKey;

            //不存在，新建
            if (!CacheDic.ContainsKey(cacheKey))
            {
                Dictionary<TKey, TValue> dic = new Dictionary<TKey, TValue>();
                CacheDic[cacheKey] = dic;
            }
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="action"></param>
        public void Set(TKey key, TValue value)
        {
            Dictionary<TKey, TValue> dic = Get();
            dic[key] = value;
        }

        /// <summary>
        /// 取出缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        public Dictionary<TKey, TValue> Get()
        {
            if (CacheDic.ContainsKey(CacheKey))
            {
                return CacheDic[CacheKey];
            }
            throw new Exception("未找到相应缓存，请确保其存在");
        }

        /// <summary>
        /// 取出缓存的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public TValue Get(TKey key)
        {
            Dictionary<TKey, TValue> dic = Get();
            if (dic.ContainsKey(key))
            {
                return dic[key];
            }
            return default(TValue);
        }

        /// <summary>
        /// 是否存在Key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool ContainsKey(TKey key)
        {
            Dictionary<TKey, TValue> dic = Get();
            return dic.ContainsKey(key);
        }

        /// <summary>
        /// 如果缓存了一个委托，取之
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private Delegate GetDelegate(TKey key)
        {
            TValue action = Get(key);
            if (action != null && action is Delegate)
            {
                Delegate func = action as Delegate;
                return func;
            }
            return null;
        }

        /// <summary>
        /// 直接调用缓存的方法
        /// </summary>
        /// <param name="key"></param>
        /// <param name="args">参数</param>
        public void DoAction(TKey key, params object[] args)
        {
            Delegate action = GetDelegate(key);
            if (action != null)
            {
                action.DynamicInvoke(args);                
            }
        }

        /// <summary>
        /// 直接调用缓存的方法(有返回值）
        /// </summary>
        /// <typeparam name="T">返回值类型</typeparam>
        /// <param name="key"></param>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public T DoFunction<T>(TKey key, params object[] args)
        {
            Delegate action = GetDelegate(key);
            if (action != null)
            {
                object o = action.DynamicInvoke(args);
                if (o is T)
                {
                    return (T)o;
                }
            }
            return default(T);
        }

        /// <summary>
        /// 清除缓存本身
        /// </summary>
        public void Clear()
        {
            if (CacheDic.ContainsKey(CacheKey))
            {
                CacheDic.Remove(CacheKey);                
            }
        }

        /// <summary>
        /// 清除缓存的Dictionary的内容
        /// </summary>
        public void ClearContent()
        {
            if (CacheDic.ContainsKey(CacheKey))
            {
                CacheDic[CacheKey].Clear();
            }
        }
    }
}
